<?php

namespace App\Http\Controllers\Seller;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
// use Hash;
use Crypt;
use Gregwar\Captcha\CaptchaBuilder;
use Session;

class UserController extends Controller
{
    //add
    public function add()
    {
        return view('home.seller.add');
    }
    public function insert(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        //查询数据库是否有该用户名
    $this -> validate($request,[
        'password' => 'min:6',
        'repassword' => 'same:password',
        'phone' => 'regex:/[0-9]{11}/|min:11'
        ],[
            'passowrd.min' => '密码最少6位',
            'repassword.same' => '密码与确认密码不一致',
            'phone.regex' => '手机格式不正确',
            'phone.min' => '手机格式不正确'
        ]
        );
        // dd($data);

    $res = DB::table('seller') -> where('name',$data['name']) -> first();
    // dd($res);
      if($res)
      {
        return back();
      }
      // dd($data);
        //删除repassword
        unset($data['repassword']);
        //加密密码
        // $data['password'] = Hash::make($data['password']);
        $data['password'] = Crypt::encrypt($data['password']);
      
        $res = DB::table('seller') -> insert($data);
        if($res)
        {
            echo '<script>alert("注册成功");window.location.href="/home/seller/denglu";</script>';
        }else{
             echo '<script>alert("注册失败");window.location.href="/home/seller/add";</script>';
        }
     
    }
    public function checkuser(Request $request)
    {

        $data = $request -> except('_token');
        //查询数据库是否有该用户名
        $res = DB::table('seller') -> where('name',$data['name']) -> first();
        if($res)
        {
            return $msg = '该用户已存在';
        }else{
            return $msg = null;
        }

    }
    public function denglu()
    {
        return view('home.seller.denglu');
    }
    public function dodenglu(Request $request)
    {
        //检查验证码
        $code = $request -> input('code');
        if($code != session('code'))
        {
            return back() ->with(['error' => '验证码有误']);
        }
        $data = $request -> except('_token');

        // dd($data);
        //查询数据
        $password = DB::table('seller') -> where('name',$data['name']) -> first() -> password;
        $res = DB::table('seller') -> where('name',$data['name']) -> first();
        // dd($res);
        // Session::put('id', $res->id);
        // Session::put('id', $res->name);
        // Session::put('id', $res->status);
        session()->flush();
        Session(['id' => $res -> id]);
        Session(['name' => $res -> name]);
        Session(['maijia' => 1]);

        //解密  密码
        $pwd = Crypt::decrypt($password);
        if($data['password'] == $pwd)
        {
             return  redirect ('/home/index/index')->with(['success' => '登录成功']);
        }
        // dd($password);
    }
    public function createCode()
    {
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 200, $height = 60, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('code', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }
}

